package com.ruoyi.kanfang.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.kanfang.domain.BuildingInfo;
import com.ruoyi.kanfang.service.IBuildingInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 楼盘小区Controller
 * 
 * @author limin
 * @date 2024-04-28
 */
@RestController
@RequestMapping("/kanfang/building")
public class BuildingInfoController extends BaseController
{
    @Autowired
    private IBuildingInfoService buildingInfoService;

    /**
     * 查询楼盘小区列表
     */
//    @PreAuthorize("@ss.hasPermi('kanfang:building:list')")
    @GetMapping("/list")
    @Anonymous
    public TableDataInfo list(BuildingInfo buildingInfo)
    {
        startPage();
        List<BuildingInfo> list = buildingInfoService.selectBuildingInfoList(buildingInfo);
        return getDataTable(list);
    }

    /**
     * 导出楼盘小区列表
     */
    @PreAuthorize("@ss.hasPermi('kanfang:building:export')")
    @Log(title = "楼盘小区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BuildingInfo buildingInfo)
    {
        List<BuildingInfo> list = buildingInfoService.selectBuildingInfoList(buildingInfo);
        ExcelUtil<BuildingInfo> util = new ExcelUtil<BuildingInfo>(BuildingInfo.class);
        util.exportExcel(response, list, "楼盘小区数据");
    }

    /**
     * 获取楼盘小区详细信息
     */
    @PreAuthorize("@ss.hasPermi('kanfang:building:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(buildingInfoService.selectBuildingInfoById(id));
    }

    /**
     * 新增楼盘小区
     */
    @PreAuthorize("@ss.hasPermi('kanfang:building:add')")
    @Log(title = "楼盘小区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BuildingInfo buildingInfo)
    {
        return toAjax(buildingInfoService.insertBuildingInfo(buildingInfo));
    }

    /**
     * 修改楼盘小区
     */
    @PreAuthorize("@ss.hasPermi('kanfang:building:edit')")
    @Log(title = "楼盘小区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BuildingInfo buildingInfo)
    {
        return toAjax(buildingInfoService.updateBuildingInfo(buildingInfo));
    }

    /**
     * 删除楼盘小区
     */
    @PreAuthorize("@ss.hasPermi('kanfang:building:remove')")
    @Log(title = "楼盘小区", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(buildingInfoService.deleteBuildingInfoByIds(ids));
    }

    @GetMapping("/initData/{dataSource}")
    @Anonymous
    public AjaxResult initData(@PathVariable("dataSource") Integer dataSource){
        // 初始化数据
        if (dataSource != 1){
            //目前仅有贝壳一个数据源
            return AjaxResult.warn("暂不支持该数据源");
        }
        return buildingInfoService.initData(dataSource);
    }
}
